package de.slothsoft.commons.swing.editorview;

public abstract class AbstractEditor<E> extends AbstractPart implements
		Editor<E> {

	private static final long serialVersionUID = 1L;

	private EditorViewPanel parent;
	private E model;

	@Override
	public void init(EditorViewPanel parent, E value) {
		this.parent = parent;
		this.model = value;
		createLayout();
		updateControlsFromValue(value);
	}

	protected abstract void createLayout();

	protected abstract void updateControlsFromValue(E value);

	protected abstract void updateValueFromControls(E value);

	protected abstract void persistValue(E value);

	public void closeEditor() {
		getEditorViewParent().closeEditor(this);
	}

	public void saveEditor() {
		updateValueFromControls(this.model);
		persistValue(this.model);
	}

	@Override
	public E getModel() {
		return this.model;
	}

	protected void setModel(E model) {
		this.model = model;
	}

	protected EditorViewPanel getEditorViewParent() {
		return this.parent;
	}

}
